﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Operator_OpenBankAccount : System.Web.UI.Page
{
    BankDataDataContext db = new BankDataDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        Company currentCompany = getCurrentCompany();
        if (currentCompany != null)
        {
            CompanyName.Text = currentCompany.CompanyName;
        }
        string currencyId = null;
        if (Session["currencyId"] != null)
        {
            currencyId = Session["currencyId"].ToString();
        }
        if (currencyId != null)
        {
            CurrencyList.SelectedValue = currencyId;
        }
    }

    protected Company getCurrentCompany()
    {
        string companyId = null;
        if (Session["companyId"] != null)
        {
            companyId = Session["companyId"].ToString();
        }
        if (companyId != null)
        {
            var companies = from company in db.Companies where company.CompanyId == Convert.ToInt32(companyId) select company;
            return companies.First();
        }
        return null;
    }

    protected void CreateBankAccountButton_Click(object sender, EventArgs e)
    {
        BankAccount bankAccount = new BankAccount();
        bankAccount.CurrencyId = Convert.ToInt32(CurrencyList.SelectedValue);
        bankAccount.Amount = 0;
        db.BankAccounts.InsertOnSubmit(bankAccount);
        db.SubmitChanges();
        BankAccountCompany bankAccountCompany = new BankAccountCompany();
        bankAccountCompany.BankAccountId = bankAccount.BankAccountId;
        bankAccountCompany.CompanyId = getCurrentCompany().CompanyId;
        db.BankAccountCompanies.InsertOnSubmit(bankAccountCompany);
        db.SubmitChanges();
        string requestId = null;
        if (Session["requestId"] != null)
        {
            requestId = Session["requestId"].ToString();
        }
        if (requestId == null)
        {
            Response.Redirect("~/Operator/BankAccounts.aspx");
        }
        else
        {
            Response.Redirect("~/Operator/AddContract.aspx");
        }
    }
}